﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using l1cs.server.model.gametime;
using l1cs.server.model;
using l1cs.server.model.Instance;
using l1cs.server.datatables;

namespace l1cs.server
{
    public class LightTimeController
    {
        private static Logger _log = Logger.getLogger( typeof( LightTimeController ).FullName );

        private static LightTimeController _instance;

        private bool isSpawn = false;

        public static LightTimeController getInstance()
        {
            if ( _instance == null )
            {
                _instance = new LightTimeController();
            }
            return _instance;
        }

        public void threadSub( Object state )
        {
            while ( true )
            {
                checkLightTime();
                Thread.Sleep( 60000 );
            }
        }

        private void checkLightTime()
        {
            long serverTime = L1GameTimeClock.getInstance().currentTime().getSeconds();
            long nowTime = serverTime % 86400;
            if ( nowTime >= ( ( 5 * 3600 ) + 3300 ) && nowTime < ( ( 17 * 3600 ) + 3300 ) )
            { 
                // 5:55~17:55
                if ( isSpawn )
                {
                    isSpawn = false;
                    foreach ( L1Object l1object in L1World.getInstance().getObject() )
                    {
                        if ( l1object is L1FieldObjectInstance )
                        {
                            L1FieldObjectInstance npc = (L1FieldObjectInstance)l1object;
                            if ( ( npc.getNpcTemplate().get_npcId() == 81177
                                || npc.getNpcTemplate().get_npcId() == 81178
                                || npc.getNpcTemplate().get_npcId() == 81179
                                || npc.getNpcTemplate().get_npcId() == 81180
                                || npc.getNpcTemplate().get_npcId() == 81181 )
                                && ( npc.getMapId() == 0 || npc.getMapId() == 4 ) )
                            {
                                npc.deleteMe();
                            }
                        }
                    }
                }
            }
            else if ( ( nowTime >= ( ( 17 * 3600 ) + 3300 ) && nowTime <= 24 * 3600 )
                  || ( nowTime >= 0 * 3600 && nowTime < ( ( 5 * 3600 ) + 3300 ) ) )
            { 
                // 17:55~24:00,0:00~5:55
                if ( !isSpawn )
                {
                    isSpawn = true;
                    LightSpawnTable.getInstance();
                }
            }
        }
    }
}
